<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>hsvapalette.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_ui_hsvapalette.js.html">hsvapalette.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview An HSVA (hue/saturation/value/alpha) color palette/picker
<a name="line17"></a> * implementation.
<a name="line18"></a> * Without the styles from the demo css file, only a hex color label and input
<a name="line19"></a> * field show up.
<a name="line20"></a> *
<a name="line21"></a> * @author chrisn@google.com (Chris Nokleberg)
<a name="line22"></a> * @see ../demos/hsvapalette.html
<a name="line23"></a> */
<a name="line24"></a>
<a name="line25"></a>goog.provide(&#39;goog.ui.HsvaPalette&#39;);
<a name="line26"></a>
<a name="line27"></a>goog.require(&#39;goog.array&#39;);
<a name="line28"></a>goog.require(&#39;goog.color.alpha&#39;);
<a name="line29"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line30"></a>goog.require(&#39;goog.events&#39;);
<a name="line31"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line32"></a>goog.require(&#39;goog.style&#39;);
<a name="line33"></a>goog.require(&#39;goog.ui.Component&#39;);
<a name="line34"></a>goog.require(&#39;goog.ui.HsvPalette&#39;);
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>/**
<a name="line39"></a> * Creates an HSVA palette. Allows a user to select the hue, saturation,
<a name="line40"></a> * value/brightness and alpha/opacity.
<a name="line41"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
<a name="line42"></a> * @param {string=} opt_color Optional initial color, without alpha (default is
<a name="line43"></a> *     red).
<a name="line44"></a> * @param {number=} opt_alpha Optional initial alpha (default is 1).
<a name="line45"></a> * @param {string=} opt_class Optional base for creating classnames (default is
<a name="line46"></a> *     &#39;goog-hsva-palette&#39;).
<a name="line47"></a> * @extends {goog.ui.HsvPalette}
<a name="line48"></a> * @constructor
<a name="line49"></a> * @final
<a name="line50"></a> */
<a name="line51"></a>goog.ui.HsvaPalette = function(opt_domHelper, opt_color, opt_alpha, opt_class) {
<a name="line52"></a>  goog.ui.HsvaPalette.base(
<a name="line53"></a>      this, &#39;constructor&#39;, opt_domHelper, opt_color, opt_class);
<a name="line54"></a>
<a name="line55"></a>  /**
<a name="line56"></a>   * Alpha transparency of the currently selected color, in [0, 1]. When
<a name="line57"></a>   * undefined, the palette will behave as a non-transparent HSV palette,
<a name="line58"></a>   * assuming full opacity.
<a name="line59"></a>   * @type {number}
<a name="line60"></a>   * @private
<a name="line61"></a>   */
<a name="line62"></a>  this.alpha_ = goog.isDef(opt_alpha) ? opt_alpha : 1;
<a name="line63"></a>
<a name="line64"></a>  /**
<a name="line65"></a>   * @override
<a name="line66"></a>   */
<a name="line67"></a>  this.className = opt_class || goog.getCssName(&#39;goog-hsva-palette&#39;);
<a name="line68"></a>};
<a name="line69"></a>goog.inherits(goog.ui.HsvaPalette, goog.ui.HsvPalette);
<a name="line70"></a>
<a name="line71"></a>
<a name="line72"></a>/**
<a name="line73"></a> * DOM element representing the alpha background image.
<a name="line74"></a> * @type {Element}
<a name="line75"></a> * @private
<a name="line76"></a> */
<a name="line77"></a>goog.ui.HsvaPalette.prototype.aImageEl_;
<a name="line78"></a>
<a name="line79"></a>
<a name="line80"></a>/**
<a name="line81"></a> * DOM element representing the alpha handle.
<a name="line82"></a> * @type {Element}
<a name="line83"></a> * @private
<a name="line84"></a> */
<a name="line85"></a>goog.ui.HsvaPalette.prototype.aHandleEl_;
<a name="line86"></a>
<a name="line87"></a>
<a name="line88"></a>/**
<a name="line89"></a> * DOM element representing the swatch backdrop image.
<a name="line90"></a> * @type {Element}
<a name="line91"></a> * @private
<a name="line92"></a> */
<a name="line93"></a>goog.ui.HsvaPalette.prototype.swatchBackdropEl_;
<a name="line94"></a>
<a name="line95"></a>
<a name="line96"></a>/** @override */
<a name="line97"></a>goog.ui.HsvaPalette.prototype.getAlpha = function() {
<a name="line98"></a>  return this.alpha_;
<a name="line99"></a>};
<a name="line100"></a>
<a name="line101"></a>
<a name="line102"></a>/**
<a name="line103"></a> * Sets which color is selected and update the UI. The passed color should be
<a name="line104"></a> * in #rrggbb format. The alpha value will be set to 1.
<a name="line105"></a> * @param {number} alpha The selected alpha value, in [0, 1].
<a name="line106"></a> */
<a name="line107"></a>goog.ui.HsvaPalette.prototype.setAlpha = function(alpha) {
<a name="line108"></a>  this.setColorAlphaHelper_(this.color_, alpha);
<a name="line109"></a>};
<a name="line110"></a>
<a name="line111"></a>
<a name="line112"></a>/**
<a name="line113"></a> * Sets which color is selected and update the UI. The passed color should be
<a name="line114"></a> * in #rrggbb format. The alpha value will be set to 1.
<a name="line115"></a> * @param {string} color The selected color.
<a name="line116"></a> * @override
<a name="line117"></a> */
<a name="line118"></a>goog.ui.HsvaPalette.prototype.setColor = function(color) {
<a name="line119"></a>  this.setColorAlphaHelper_(color, 1);
<a name="line120"></a>};
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * Gets the color that is currently selected in this color picker, in #rrggbbaa
<a name="line125"></a> * format.
<a name="line126"></a> * @return {string} The string of the selected color with alpha.
<a name="line127"></a> */
<a name="line128"></a>goog.ui.HsvaPalette.prototype.getColorRgbaHex = function() {
<a name="line129"></a>  var alphaHex = Math.floor(this.alpha_ * 255).toString(16);
<a name="line130"></a>  return this.color_ + (alphaHex.length == 1 ? &#39;0&#39; + alphaHex : alphaHex);
<a name="line131"></a>};
<a name="line132"></a>
<a name="line133"></a>
<a name="line134"></a>/**
<a name="line135"></a> * Sets which color is selected and update the UI. The passed color should be
<a name="line136"></a> * in #rrggbbaa format. The alpha value will be set to 1.
<a name="line137"></a> * @param {string} color The selected color with alpha.
<a name="line138"></a> */
<a name="line139"></a>goog.ui.HsvaPalette.prototype.setColorRgbaHex = function(color) {
<a name="line140"></a>  var parsed = goog.ui.HsvaPalette.parseColorRgbaHex_(color);
<a name="line141"></a>  this.setColorAlphaHelper_(parsed[0], parsed[1]);
<a name="line142"></a>};
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/**
<a name="line146"></a> * Sets which color and alpha value are selected and update the UI. The passed
<a name="line147"></a> * color should be in #rrggbb format.
<a name="line148"></a> * @param {string} color The selected color in #rrggbb format.
<a name="line149"></a> * @param {number} alpha The selected alpha value, in [0, 1].
<a name="line150"></a> * @private
<a name="line151"></a> */
<a name="line152"></a>goog.ui.HsvaPalette.prototype.setColorAlphaHelper_ = function(color, alpha) {
<a name="line153"></a>  var colorChange = this.color_ != color;
<a name="line154"></a>  var alphaChange = this.alpha_ != alpha;
<a name="line155"></a>  this.alpha_ = alpha;
<a name="line156"></a>  this.color_ = color;
<a name="line157"></a>  if (colorChange) {
<a name="line158"></a>    // This is to prevent multiple event dispatches.
<a name="line159"></a>    this.setColorInternal(color);
<a name="line160"></a>  }
<a name="line161"></a>  if (colorChange || alphaChange) {
<a name="line162"></a>    this.updateUi();
<a name="line163"></a>    this.dispatchEvent(goog.ui.Component.EventType.ACTION);
<a name="line164"></a>  }
<a name="line165"></a>};
<a name="line166"></a>
<a name="line167"></a>
<a name="line168"></a>/** @override */
<a name="line169"></a>goog.ui.HsvaPalette.prototype.createDom = function() {
<a name="line170"></a>  goog.ui.HsvaPalette.base(this, &#39;createDom&#39;);
<a name="line171"></a>
<a name="line172"></a>  var dom = this.getDomHelper();
<a name="line173"></a>  this.aImageEl_ = dom.createDom(
<a name="line174"></a>      goog.dom.TagName.DIV, goog.getCssName(this.className, &#39;a-image&#39;));
<a name="line175"></a>  this.aHandleEl_ = dom.createDom(
<a name="line176"></a>      goog.dom.TagName.DIV, goog.getCssName(this.className, &#39;a-handle&#39;));
<a name="line177"></a>  this.swatchBackdropEl_ = dom.createDom(
<a name="line178"></a>      goog.dom.TagName.DIV, goog.getCssName(this.className, &#39;swatch-backdrop&#39;));
<a name="line179"></a>  var element = this.getElement();
<a name="line180"></a>  dom.appendChild(element, this.aImageEl_);
<a name="line181"></a>  dom.appendChild(element, this.aHandleEl_);
<a name="line182"></a>  dom.appendChild(element, this.swatchBackdropEl_);
<a name="line183"></a>};
<a name="line184"></a>
<a name="line185"></a>
<a name="line186"></a>/** @override */
<a name="line187"></a>goog.ui.HsvaPalette.prototype.disposeInternal = function() {
<a name="line188"></a>  goog.ui.HsvaPalette.base(this, &#39;disposeInternal&#39;);
<a name="line189"></a>
<a name="line190"></a>  delete this.aImageEl_;
<a name="line191"></a>  delete this.aHandleEl_;
<a name="line192"></a>  delete this.swatchBackdropEl_;
<a name="line193"></a>};
<a name="line194"></a>
<a name="line195"></a>
<a name="line196"></a>/** @override */
<a name="line197"></a>goog.ui.HsvaPalette.prototype.updateUi = function() {
<a name="line198"></a>  goog.ui.HsvaPalette.base(this, &#39;updateUi&#39;);
<a name="line199"></a>  if (this.isInDocument()) {
<a name="line200"></a>    var a = this.alpha_ * 255;
<a name="line201"></a>    var top = this.aImageEl_.offsetTop -
<a name="line202"></a>        Math.floor(this.aHandleEl_.offsetHeight / 2) +
<a name="line203"></a>        this.aImageEl_.offsetHeight * ((255 - a) / 255);
<a name="line204"></a>    this.aHandleEl_.style.top = top + &#39;px&#39;;
<a name="line205"></a>    this.aImageEl_.style.backgroundColor = this.color_;
<a name="line206"></a>    goog.style.setOpacity(this.swatchElement, a / 255);
<a name="line207"></a>  }
<a name="line208"></a>};
<a name="line209"></a>
<a name="line210"></a>
<a name="line211"></a>/** @override */
<a name="line212"></a>goog.ui.HsvaPalette.prototype.updateInput = function() {
<a name="line213"></a>  if (!goog.array.equals([this.color_, this.alpha_],
<a name="line214"></a>      goog.ui.HsvaPalette.parseUserInput_(this.inputElement.value))) {
<a name="line215"></a>    this.inputElement.value = this.getColorRgbaHex();
<a name="line216"></a>  }
<a name="line217"></a>};
<a name="line218"></a>
<a name="line219"></a>
<a name="line220"></a>/** @override */
<a name="line221"></a>goog.ui.HsvaPalette.prototype.handleMouseDown = function(e) {
<a name="line222"></a>  goog.ui.HsvaPalette.base(this, &#39;handleMouseDown&#39;, e);
<a name="line223"></a>  if (e.target == this.aImageEl_ || e.target == this.aHandleEl_) {
<a name="line224"></a>    // Setup value change listeners
<a name="line225"></a>    var b = goog.style.getBounds(this.valueBackgroundImageElement);
<a name="line226"></a>    this.handleMouseMoveA_(b, e);
<a name="line227"></a>    this.mouseMoveListener = goog.events.listen(
<a name="line228"></a>        this.getDomHelper().getDocument(),
<a name="line229"></a>        goog.events.EventType.MOUSEMOVE,
<a name="line230"></a>        goog.bind(this.handleMouseMoveA_, this, b));
<a name="line231"></a>    this.mouseUpListener = goog.events.listen(
<a name="line232"></a>        this.getDomHelper().getDocument(),
<a name="line233"></a>        goog.events.EventType.MOUSEUP, this.handleMouseUp, false, this);
<a name="line234"></a>  }
<a name="line235"></a>};
<a name="line236"></a>
<a name="line237"></a>
<a name="line238"></a>/**
<a name="line239"></a> * Handles mousemove events on the document once a drag operation on the alpha
<a name="line240"></a> * slider has started.
<a name="line241"></a> * @param {goog.math.Rect} b Boundaries of the value slider object at the start
<a name="line242"></a> *     of the drag operation.
<a name="line243"></a> * @param {goog.events.Event} e Event object.
<a name="line244"></a> * @private
<a name="line245"></a> */
<a name="line246"></a>goog.ui.HsvaPalette.prototype.handleMouseMoveA_ = function(b, e) {
<a name="line247"></a>  e.preventDefault();
<a name="line248"></a>  var vportPos = this.getDomHelper().getDocumentScroll();
<a name="line249"></a>  var newA = (b.top + b.height - Math.min(
<a name="line250"></a>      Math.max(vportPos.y + e.clientY, b.top),
<a name="line251"></a>      b.top + b.height)) / b.height;
<a name="line252"></a>  this.setAlpha(newA);
<a name="line253"></a>};
<a name="line254"></a>
<a name="line255"></a>
<a name="line256"></a>/** @override */
<a name="line257"></a>goog.ui.HsvaPalette.prototype.handleInput = function(e) {
<a name="line258"></a>  var parsed = goog.ui.HsvaPalette.parseUserInput_(this.inputElement.value);
<a name="line259"></a>  if (parsed) {
<a name="line260"></a>    this.setColorAlphaHelper_(parsed[0], parsed[1]);
<a name="line261"></a>  }
<a name="line262"></a>};
<a name="line263"></a>
<a name="line264"></a>
<a name="line265"></a>/**
<a name="line266"></a> * Parses an #rrggbb or #rrggbbaa color string.
<a name="line267"></a> * @param {string} value User-entered color value.
<a name="line268"></a> * @return {Array} A two element array [color, alpha], where color is #rrggbb
<a name="line269"></a> *     and alpha is in [0, 1]. Null if the argument was invalid.
<a name="line270"></a> * @private
<a name="line271"></a> */
<a name="line272"></a>goog.ui.HsvaPalette.parseUserInput_ = function(value) {
<a name="line273"></a>  if (/^#?[0-9a-f]{8}$/i.test(value)) {
<a name="line274"></a>    return goog.ui.HsvaPalette.parseColorRgbaHex_(value);
<a name="line275"></a>  } else if (/^#?[0-9a-f]{6}$/i.test(value)) {
<a name="line276"></a>    return [value, 1];
<a name="line277"></a>  }
<a name="line278"></a>  return null;
<a name="line279"></a>};
<a name="line280"></a>
<a name="line281"></a>
<a name="line282"></a>/**
<a name="line283"></a> * Parses a #rrggbbaa color string.
<a name="line284"></a> * @param {string} color The color and alpha in #rrggbbaa format.
<a name="line285"></a> * @return {!Array} A two element array [color, alpha], where color is #rrggbb
<a name="line286"></a> *     and alpha is in [0, 1].
<a name="line287"></a> * @private
<a name="line288"></a> */
<a name="line289"></a>goog.ui.HsvaPalette.parseColorRgbaHex_ = function(color) {
<a name="line290"></a>  var hex = goog.color.alpha.parse(color).hex;
<a name="line291"></a>  return [
<a name="line292"></a>    goog.color.alpha.extractHexColor(hex),
<a name="line293"></a>    parseInt(goog.color.alpha.extractAlpha(hex), 16) / 255
<a name="line294"></a>  ];
<a name="line295"></a>};
</pre>


</body>
</html>
